package com.meteor.downloader.videodownloader.adapter

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.text.format.Formatter
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import coil.decode.VideoFrameDecoder
import coil.load
import coil.transform.RoundedCornersTransformation
import com.meteor.downloader.videodownloader.R
import com.meteor.downloader.videodownloader.databinding.ItemLibraryMdmItemBinding
import com.meteor.downloader.videodownloader.imageCenterCropMdm
import com.meteor.downloader.videodownloader.mdmGetPxDp
import com.meteor.downloader.videodownloader.mdmdata.DataMdmFile
import com.meteor.downloader.videodownloader.utils.MdmTotalUtils

class AdapterLibraryItem(
    private val mContext: Context,
    private val mVideoList: MutableList<DataMdmFile>,
    private val mItemClick: (Int) -> Unit,
    private val mRenameClick: (Int) -> Unit,
    private val mShareClick: (Int) -> Unit,
    private val mDeleteClick: (Int) -> Unit
) : Adapter<AdapterLibraryItem.HolderItem>() {
    inner class HolderItem(val mBd: ItemLibraryMdmItemBinding) : ViewHolder(mBd.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HolderItem {
        val binding =
            ItemLibraryMdmItemBinding.inflate(LayoutInflater.from(mContext), parent, false)
        val holderItem = HolderItem(binding)
        holderItem.itemView.setOnClickListener {
            mItemClick(holderItem.absoluteAdapterPosition)
        }
        binding.ivMdmTools.setOnClickListener {
            val popup = PopupWindow(mContext).apply {
                width = ViewGroup.LayoutParams.WRAP_CONTENT
                height = ViewGroup.LayoutParams.WRAP_CONTENT
                val view = LayoutInflater.from(mContext)
                    .inflate(R.layout.item_library_tools_pop, null, false).apply {
                        findViewById<LinearLayout>(R.id.llRename).setOnClickListener {
                            dismiss()
                            mRenameClick(holderItem.absoluteAdapterPosition)
                        }
                        findViewById<LinearLayout>(R.id.llShare).setOnClickListener {
                            dismiss()
                            mShareClick(holderItem.absoluteAdapterPosition)
                        }
                        findViewById<LinearLayout>(R.id.llDelete).setOnClickListener {
                            dismiss()
                            mDeleteClick(holderItem.absoluteAdapterPosition)
                        }
                    }
                contentView = view
                isFocusable = true
                setBackgroundDrawable(ColorDrawable(0x00000000))
                isOutsideTouchable = true
            }
            popup.showAsDropDown(it, -36f.mdmGetPxDp().toInt(), 0, Gravity.CENTER)
        }

        return holderItem
    }

    override fun getItemCount(): Int {
        return mVideoList.size
    }

    override fun onBindViewHolder(holder: HolderItem, position: Int) {
        val video = mVideoList[holder.absoluteAdapterPosition]
        holder.mBd.apply {
            ivMdmImage.imageCenterCropMdm(16f)
            ivMdmImage.load(video.mFileMdm) {
                transformations(RoundedCornersTransformation(16f.mdmGetPxDp()))
                if (video.mFileMdm.extension.equals("mp4", true)) {
                    decoderFactory { result, options, _ ->
                        VideoFrameDecoder(
                            result.source,
                            options
                        )
                    }
                }
                placeholder(R.drawable.mdm_shape_16dp_dcdcdc)
                error(R.drawable.mdm_shape_16dp_dcdcdc)
            }
            tvMdmName.text = video.mFileMdm.name
            tvMdmSize.text = Formatter.formatFileSize(mContext, video.mFileMdm.length()) + "\n" +
                    MdmTotalUtils.getFileDateMdm(video.mFileMdm.lastModified())
        }
    }
}